package com.woniu.springbootrbac.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.springbootrbac.service.PerService;
import com.woniu.springbootrbac.vo.PerVo;
import com.woniu.springbootrbac.vo.UserAo;
import com.woniu.springbootrbac.entity.SysUser;
import com.woniu.springbootrbac.dto.UserDto;
import com.woniu.springbootrbac.service.UserService;
import com.woniu.springbootrbac.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService service;

    @Autowired
    private PerService perVo;

    @PostMapping("loginDo")
    public Result login(@RequestBody UserDto dto){
        SysUser login = service.login(dto);
        if (login != null) {
            List<PerVo> perVos = perVo.selectMenuByTel(dto.getTel(), 0);
            return Result.ok().data("menuList",perVos);
        } else {
            return Result.error();
        }
    }

    @PostMapping("SelByLike")
    public Result selByLike(@RequestBody UserDto dto){
        PageHelper.startPage(dto.getCurrentPage(), dto.getPageSize(), true);
        List<UserAo> userAos = service.selByLike(dto);
        PageInfo<UserAo> pageInfo = new PageInfo<>(userAos);
        return Result.ok().data("list",userAos).data("total",pageInfo.getTotal());
    }

    @PostMapping("")
    public Result add(@RequestBody SysUser user){
        int i = service.insertSelective(user);
        return Result.ok();
    }

    @GetMapping("{id}")
    public Result selOne(@PathVariable Long id){
        SysUser sysUser = service.selectByPrimaryKey(id);
        return Result.ok().data("user",sysUser);
    }

    @PutMapping("")
    public Result update(@RequestBody SysUser user){
        int i = service.updateByPrimaryKeySelective(user);
        return Result.ok();
    }

    @DeleteMapping("{id}")
    public Result del(@PathVariable Long id){
        int i = service.deleteByPrimaryKey(id);
        return Result.ok();
    }
}
